package com.ruoyi.jihuo.api;

import com.ruoyi.jihuo.domain.JhTmAccount;
import com.taobao.top.*;
import com.taobao.top.defaultability.Defaultability;
import com.taobao.top.defaultability.request.TaobaoPictureUploadRequest;
import com.taobao.top.defaultability.response.TaobaoPictureUploadResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.Map;


@Component
public class TaoApi3 implements TaoApi {
    private static final Logger log = LoggerFactory.getLogger(TaoApi3.class);

    @Autowired
    private ApiStrategyContext apiStrategyContext;

    // http://gw.api.taobao.com/router/rest
    private static final String SERVER_URL = "http://gw.api.taobao.com/router/rest";


    @Override
    public String callTaoApi(String api, Map<String, Object> paramMap, JhTmAccount jhTmAccount) {
        // create Client
        TopApiClient client = new DefaultTopApiClient(jhTmAccount.getJhAppKey(), jhTmAccount.getJhAppSecret(), SERVER_URL);

        ApiStrategy strategy = apiStrategyContext.getStrategy(api);
        if (strategy == null) {
            throw new IllegalArgumentException("Unsupported API: " + api);
        }
        BaseTopApiRequest request = strategy.setParams(api, paramMap);
        BaseTopApiResponse response = null;
        try {
            response = strategy.execute(client, request, jhTmAccount);
        } catch (IOException e) {
            log.error("调用淘宝api异常", e);
            throw new RuntimeException(e);
        }

        if (!response.isSuccess()) {
            log.error(response.getSubMsg());
            if (!response.getSubMsg().contains("已存在产品")){
                throw new RuntimeException(response.getSubMsg());
            }
        }

        log.info("response: {}", response.getBody());
        // 只取body的话会丢一些数据
        return response.getBody();
    }


    @Override
    public String uploadImage(String api, Map<String, Object> paramMap, JhTmAccount jhTmAccount) {
        // create Client
        TopApiClient client = new DefaultTopApiClient(jhTmAccount.getJhAppKey(), jhTmAccount.getJhAppSecret(), SERVER_URL);
        Defaultability apiPackage = new Defaultability(client);
        // create domain

        // create request
        TaobaoPictureUploadRequest request = new TaobaoPictureUploadRequest();
        request.setPictureId(232323L);
        request.setTitle("2333.png");
        request.setClientType("client:computer");
        request.setPictureCategoryId((long) paramMap.get("picture_category_id"));
        request.setImg(new TopFileItem(new File((String) paramMap.get("file_path"))));
        request.setImageInputTitle("2333.png");

        TaobaoPictureUploadResponse response = null;
        try {
            response = apiPackage.taobaoPictureUpload(request, jhTmAccount.getJhAppSession());
        } catch (IOException e) {
            log.error("调用淘宝api上传图片异常", e);
            throw new RuntimeException(e);
        }
        if (!response.isSuccess()) {
            log.error(response.getSubMsg());
        }

        log.info("response: {}", response.getBody());
        // 只取body的话会丢一些数据
        return response.getBody();
    }


}
